package com.aos.eap.activity;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.EEDownRecipeBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class EE_DownRecipeActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.etBdCode)
    FreeUI_ClearEditText etBdCode;
    @BindView(R.id.tvBdVersion)
    TextView tvBdVersion;
    @BindView(R.id.tvCurrentType)
    TextView tvCurrentType;
    @BindView(R.id.tvRecipe)
    TextView tvRecipe;
    @BindView(R.id.tvRecipeVersion)
    TextView tvRecipeVersion;
    @BindView(R.id.tvRecipeType)
    TextView tvRecipeType;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private EEDownRecipeBean eeDownRecipeBean;
    private String method = "MHS";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_me_down_recipe;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, s -> MyUtils.setEditTextFocus(etLotId));
        MyUtils.setEditTextListener(etLotId, this, s -> MyUtils.setEditTextFocus(etBdCode));
        MyUtils.setEditTextListener(etBdCode, this, s -> {
            String[] values = s.split(";");
            if (values.length > 1) {
                etBdCode.setText(values[0]);
                tvBdVersion.setText(values[1]);
            }
            requestInfoByEqpId();
        });
    }

    private void requestInfoByEqpId() {
        String eqpId = etEqpId.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(eqpId)) {
            showToast("请扫描设备编号", false);
            MyUtils.setEditTextFocus(etEqpId);
            return;
        }
        String lotId = etLotId.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(lotId)) {
            showToast("请扫描批次编号", false);
            MyUtils.setEditTextFocus(etLotId);
            return;
        }
        String bdId = etBdCode.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(bdId)) {
            showToast("请扫描BD编号", false);
            MyUtils.setEditTextFocus(etBdCode);
            return;
        }
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        map.addProperty("LotId", lotId);
        map.addProperty("BDVer", tvBdVersion.getText().toString());
        map.addProperty("BDId", bdId);
        NetUtils.requestGetNet(this, "EMS_DownRecipeMELoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EEDownRecipeBean>>(
                new TypeToken<BaseResponseBean<EEDownRecipeBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<EEDownRecipeBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    eeDownRecipeBean = response.body().getResponse().getBody();
                    setPageData(eeDownRecipeBean);
                }else {
                    eeDownRecipeBean = null;
                    setPageData(new EEDownRecipeBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EEDownRecipeBean>> response) {
                super.onError(response);
                eeDownRecipeBean = null;
                setPageData(new EEDownRecipeBean());
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_DownRecipeMELoad");
            }
        });
    }
    private void setPageData(EEDownRecipeBean eeDownRecipeBean) {
        tvRecipe.setText(eeDownRecipeBean.getRecipeID());
        tvRecipeType.setText(eeDownRecipeBean.getRecipeType());
        tvRecipeVersion.setText(eeDownRecipeBean.getRecipeVersion());
        tvCurrentType.setText(eeDownRecipeBean.getEqpType());
    }
    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("ME下载Recipe");
        freeUI_titleView.setLeftListener(view -> showBackDialog(EE_DownRecipeActivity.this));
        freeUI_titleView.setRightListener("下载", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (eeDownRecipeBean == null) {
                showToast("未获取到页面信息，请重新扫描设备编号", false);
                etEqpId.setText("");
                MyUtils.setEditTextFocus(etEqpId);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", eeDownRecipeBean.getEqpID());
            map.addProperty("EqpType", eeDownRecipeBean.getEqpType());
            map.addProperty("WbLoadRcpType", method);
            map.addProperty("RecipeType", eeDownRecipeBean.getRecipeType());
            map.addProperty("RecipeID", eeDownRecipeBean.getRecipeID());
            map.addProperty("RecipeVersion", eeDownRecipeBean.getRecipeVersion());
            map.addProperty("Remark", etRemark.getText().toString().trim());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_DownRecipe", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(EE_DownRecipeActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_DownRecipe");
            }
        });
    }

    @OnClick({R.id.rbMhs, R.id.rbRam})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbMhs:
                method = "MHS";
                break;
            case R.id.rbRam:
                method = "RAM";
                break;
        }
    }
}
